package com.itheima.task;

import com.itheima.common.utils.AliOssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class ImageTask {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AliOssUtils aliOssUtil;

    @Scheduled(cron = "0 0 */1 * * ?") // 每小时执行一次
//    @Scheduled(cron = "0/50 * * * * ?")
    public void processDeleteImageTask(){
        Set<String> difference = redisTemplate.opsForSet().difference("set1", "set2");
        if (difference != null && difference.size() > 0){
            for (String diff : difference) {
                // 存在垃圾图片，则删除该图片
                aliOssUtil.delete(diff);
                redisTemplate.opsForSet().remove("set1", diff);
            }
        }
    }
}
